// CreateAccount.aspx.cs created with MonoDevelop
// User: david at 9:37 PM 8/14/2008
//

using System;
using System.Data;
using System.Web;
using System.Web.UI;
using MySql.Data.MySqlClient;
using MySql;
using MySql.Data;

namespace VolunteerTracker
{
	public partial class CreateAccount : System.Web.UI.Page
	{
		MySqlConnection con;
		MySqlCommand insert;
		
		void Page_Load()
		{
			// Populate drpTeams dropdown with team data.
			DataTable dt = dbConnect.GetTeams();
			drpTeam.DataSource = dt;
			drpTeam.DataValueField = "teamId";
			drpTeam.DataTextField = "teamName";
			drpTeam.DataBind();
		
		}
		
		/// <summary>
		/// Connect to mysql server and insert new users.
		/// </summary>
		protected void btnSubmit_Click(object sender, EventArgs e)
		{
			try
			{
				// Open connection
				con = dbConnect.GetConnection();
				insert = new MySqlCommand("insert into users (email, password, fname, lname, phone, address, zip, state, city, teamId) " + 
				                          "values (?email, ?password, ?fname, ?lname, ?phone, ?address, ?zip, ?state, ?city, ?teamId)", con);
			   con.Open();
				
				//Insert values
				MySqlParameter pemail = new MySqlParameter("?email", MySqlDbType.VarChar);
				pemail.Value = txtEmail.Text.ToString();
				insert.Parameters.Add(pemail);
				MySqlParameter ppassword = new MySqlParameter("?password", MySqlDbType.VarChar);
				ppassword.Value = txtPass.Text.ToString();
				insert.Parameters.Add(ppassword);
				MySqlParameter pfname = new MySqlParameter("?fname", MySqlDbType.VarChar);
				pfname.Value = txtFname.Text.ToString();
				insert.Parameters.Add(pfname);
				MySqlParameter plname = new MySqlParameter("?lname", MySqlDbType.VarChar);
				plname.Value = txtLname.Text.ToString();
				insert.Parameters.Add(plname);
				MySqlParameter pphone = new MySqlParameter("?phone", MySqlDbType.VarChar);
				pphone.Value = txtPhone.Text.ToString();
				insert.Parameters.Add(pphone);
				MySqlParameter paddress = new MySqlParameter("?address", MySqlDbType.VarChar);
				paddress.Value = txtAddress.Text.ToString();
				insert.Parameters.Add(paddress);
				MySqlParameter pzip = new MySqlParameter("?zip", MySqlDbType.VarChar);
				pzip.Value = txtZip.Text.ToString();
				insert.Parameters.Add(pzip);
				MySqlParameter pstate = new MySqlParameter("?state", MySqlDbType.VarChar);
				pstate.Value = txtState.Text.ToString();
				insert.Parameters.Add(pstate);
				MySqlParameter pcity = new MySqlParameter("?city", MySqlDbType.VarChar);
				pcity.Value = txtCity.Text.ToString();
				insert.Parameters.Add(pcity);
				MySqlParameter pteamId = new MySqlParameter("?teamId", MySqlDbType.Int32);
				pteamId.Value = drpTeam.SelectedItem.Value.ToString();
				insert.Parameters.Add(pteamId);
				
				//Execute
				insert.ExecuteNonQuery();
				con.Close();
				Response.Redirect("./default.aspx");
			}
			catch(Exception ee)
			{
				Response.Write(ee.ToString());
			}
		}
		
	}
}
